<?php

namespace app\api\controller\user;

use app\api\controller\AuthController;
use app\api\service\GroupOrderService;
use app\api\service\PaymentService;
use app\api\service\ResellService;

class ResellController extends AuthController
{

    /**
     * 获取我的转卖
     * */

    public function index()
    {
        $resell_status  = $this->request->param('resell_status/d',0);

        $user_id = $this->request->user['user_id'];

        $goodsList = ResellService::getUserResellList($user_id,$resell_status);


        return json(['code' =>0, 'data' => $goodsList, 'msg' =>'获取成功!']);
    }


    /**
     * 结算尾款
     * */
    public function settlement()
    {
        $param = $this->request->param();

        if (empty($param['group_order_id']) || empty($param['store_id']) || $param['payment_type']){
            return json(['code' =>0, 'msg' => '非法参数!']);
        }


        $config = PaymentService::findPaymentConfig($param['store_id'],$param['payment_type']);

        if (empty($config)){
            return  json(['code' =>0, 'msg' =>'支付异常!']);
        }

        $groupOrder = ResellService::findGroupOrder($param['group_order_id']);

        if ($groupOrder){
            return  json(['code' =>0, 'msg' => '订单不存在!']);
        }

        $real_amount = (float)$groupOrder['group_price']-(float)$groupOrder['prepayment_amount'];
        $out_trade_no = PaymentService::generateOrderNum();

        $result = GroupOrderService::saveTradeInfo($param['group_order_id'],[
                    'store_id' => $param['store_id'],
                    'real_amount' => $real_amount,
                    'trade_method' => $param['payment_type'],
                    'out_trade_no' => $out_trade_no,
        ]);

        if ($result){
            $payment_data = [
                'config' => $config,
                'trade_method' => $param['payment_type'],
                'out_trad_no' => $out_trade_no,
                'real_amount' => $real_amount,
            ];

            if ($param['payment_type'] == 1){
                $payment_data['notify_url'] = 'https://xiangyule.jcyqkj.cn/api/group/order/finish/trade/wx/notify';
            }

            if ($param['payment_type'] == 2){
                $payment_data['notify_url'] = 'https://xiangyule.jcyqkj.cn/api/group/order/finish/trade/ali/notify';
            }

            $prepay_info = PaymentService::createOrder($payment_data);

            if (!empty($prepay_info)){
                return json([
                    'code' =>1,
                    'data' => [
                        'group_order_id' => $param['group_order_id'],
                        'out_trade_no' => $out_trade_no,
                        'prepay_info' => $prepay_info
                    ],
                    'msg' => '下单成功!'
                ]);
            }
        }

        return  json(['code' =>0, 'msg' => '下单失败!']);



    }

    /**
     *  领取商品
     * */
    public function receive()
    {
        $group_order_id = $this->request->param('group_order_id/d');

        $resell_goods_id = ResellService::findResellGoodsExists($group_order_id);

        if (!empty($resell_goods_id)){
            return json(['code' =>0, 'msg' => '您已经领取过了,请勿重复领取!']);
        }

        $goods = ResellService::findGroupActivityGoods($group_order_id);


        if (empty($goods)){
            return json(['code' =>0, 'msg' => '商品信息不存在,领取失败!']);
        }

        if ($goods['prepayment_amount'] > 0){
            return json(['code' => 1, 'is_prepayment' => 'yes','msg' => '请付齐尾款!']);
        }

        $user_id = $this->request->user['user_id'];

        $result = ResellService::receiveGroupGoods($user_id,$goods);

        return $result ? json(['code' =>1, 'is_prepayment' => 'no', 'msg' => '商品领取成功!']): json(['code' =>0, 'msg' => '商品领取失败!']);
    }

    /**
     * 发起转售
     * */
    public function sponsor()
    {
        $resell_goods_id = $this->request->param('resell_goods_id/d');

        if (empty($resell_goods_id)){
            return json(['code' =>0, '非法参数!']);
        }

        $goods = ResellService::findResellGoods($resell_goods_id);

        if (empty($goods)) {
            return json(['code' =>0, 'msg'=>'商品不存在!']);
        }

        if ($goods['is_resell'] ==0){
            return json(['code' =>0,  'msg' =>'该商品不可转手!']);
        }


        $resell_count = ResellService::getResellCount($resell_goods_id);

        if ($resell_count >= $goods['resell_times']){
            return json(['code' =>0, 'msg' => '已超过该商品转手次数!']);
        }


       /* if ($goods['is_resell_coupon'] ==1){
            $user_id = $this->request->user['user_id'];
            $user_coupon_id =  ResellService::findResellCoupon($user_id);
           if (empty($user_coupon_id)){
               return json(['code' =>0, 'msg' => '转售卷不足!']);
           }
           ResellService::changeCouponStatus($user_coupon_id);
        }*/


       $result =  ResellService::addResellRecord($resell_goods_id,$goods['resell_times'],$goods['store_id']);


       return $result ? json(['code' =>1,'msg' => '转售成功!']) : json(['code' =>0,'msg' => '转售失败!']);
    }


}